<?php

namespace App\Admin\Controllers;

use App\Models\Finance;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class FinanceController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '财务管理';

    /**
     * Make a table builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $table = new Grid(new Finance());
        $table->filter(function($filter) {
            $filter->disableIdFilter();
            $filter->like('appid');
        });

        $table->column('id', __('Id'))->totalRow('合计');
        $table->column('appid', __('Appid'));
        $table->column('account_id', __('使用的账号id'));
        $table->column('install_num',__('安装数量'))->totalRow();
        $table->column('install_price',__('安装单价'));
        $table->column('cost',__('花费'))->totalRow();
        $table->column('conversion',__('转化数量'))->totalRow();
        $table->column('conversion_price',__('转化单价'))->display(function (){
            return $this->conversion?round($this->cost/$this->conversion,2):'无效';
        });
        $table->column('conversion_rate',__('转化率'))->display(function (){
           return $this->install_num?(round($this->conversion/$this->install_num,2)*100).'%':'无效';
        });
        $table->column('date',__('日期'))->datetime();

        return $table;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Finance::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('appid', __('Appid'));
        $show->field('account_id', __('使用的账号id'));
        $show->field('install_num',__('安装数量'));
        $show->field('install_price',__('安装单价'));
        $show->field('cost',__('花费'));
        $show->field('conversion',__('转换数量'));
        $show->field('date',__('日期'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Finance());

        $form->text('appid', __('Appid'));
        $form->text('account_id', __('使用的账号id'));
        $form->number('install_num',__('安装数量'));
        $form->decimal('install_price',__('安装单价'));
        $form->decimal('cost',__('花费'));
        $form->number('conversion',__('转换数量'));
        $form->datetime('date',__('日期'));

        return $form;
    }
}
